package com.offcn.webui.controller;


import com.offcn.common.response.AppResponse;
import com.offcn.webui.service.ProjectFeignService;
import com.offcn.webui.service.UserFeignService;
import com.offcn.webui.vo.resp.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private ProjectFeignService projectService;

    @Autowired
    private UserFeignService userService;

    // /projectInfo?id=20
    @RequestMapping("/projectInfo")
    public String  getDetailProject(Integer id, Model model, HttpSession session){
        AppResponse<ProjectDetailVo> response = projectService.getProjectById(id);
        ProjectDetailVo detailVo = response.getData();
        model.addAttribute("DetailVo",detailVo);
        session.setAttribute("DetailVo",detailVo);
        return "project/project";
    }

    // /confirm/returns/28/24
    @RequestMapping("/confirm/returns/{projectId}/{returnId}")
    public String findReturnInfo(@PathVariable("projectId") Integer projectId,
                                 @PathVariable("returnId") Integer returnId,
                                 Model model,HttpSession session){
        // 获取项目信息
        // 从session中获取项目信息
        ProjectDetailVo detailVo = (ProjectDetailVo)session.getAttribute("DetailVo");
        // 获取回报信息
        AppResponse<TReturn> response = projectService.getReturnById(returnId);
        TReturn tReturn = response.getData();
        // 创建返回的对象
        ReturnPayConfirmVo vo = new ReturnPayConfirmVo();
        BeanUtils.copyProperties(tReturn,vo);
        // 项目ID
        vo.setProjectId(projectId);
        // 项目名
        vo.setProjectName(detailVo.getName());
        vo.setProjectRemark(detailVo.getRemark()); // 项目描述信息
        // 目标：发起方名字
        // 通过远程调用进行查询  user ==> 通过ID获取用户信息
        AppResponse<UserRespVo> userResponse = userService.getUserById(detailVo.getMemberid());
        UserRespVo user = userResponse.getData();
        vo.setMemberName(user.getUsername()); // 发起人的名字
        vo.setMemberId(detailVo.getMemberid()); // 发起人的ID

        // 传递数据
        model.addAttribute("returnConfirm",vo);
        session.setAttribute("returnConfirm",vo);
        return "project/pay-step-1";
    }

    // /confirm/order/7
    @GetMapping("/confirm/order/{num}")
    public String confirmOrder(@PathVariable("num") Integer num,Model model,HttpSession session){
        // 判断当前用户是否登录
        UserRespVo userVo = (UserRespVo)session.getAttribute("user");
        if(userVo == null){
            // 用户没有登录
            session.setAttribute("url","project/confirm/order/" + num);
            return "redirect:/login.html";
        }
        // 登录成功
        String accessToken = userVo.getAccessToken();
        // 获取地址列表
        AppResponse<List<UserAddressVo>> response = userService.getAddressList(accessToken);
        List<UserAddressVo> addresses = response.getData();
        model.addAttribute("addresses",addresses);
        // 获取上一步的数据
        ReturnPayConfirmVo vo = (ReturnPayConfirmVo)session.getAttribute("returnConfirm");
        vo.setNum(num);
        // 总费用 = 数量 * 单价 + 运费
        vo.setTotalPrice(new BigDecimal(num * vo.getSupportmoney() + vo.getFreight()));
        session.setAttribute("returnConfirm",vo);
        return "project/pay-step-2";
    }

}
